package fzdy.task;

import fzdy.event.EventCenter;
import fzdy.struct.DevicePostData;
import fzdy.struct.MobileCache;
import fzdy.tools.SpringContextUtil;
import org.joda.time.DateTime;
import org.joda.time.Period;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.DependsOn;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component("mobileovertimechecker")
@DependsOn("eventcenter")
public class MobileOvertimeChecker {

    @Value("${overtime}")
    private int overtime;

    @Scheduled(fixedDelayString = "${taskMobileInternal}000",initialDelay = 10000)
    public void checkMobileOvertime(){
        ((EventCenter)SpringContextUtil.getBean("eventcenter")).postEvent(new DevicePostData());
        DateTime time = DateTime.now();
        ((EventCenter) SpringContextUtil.getBean("eventcenter")).postEvent(new DevicePostData());
        for(String key: MobileCache.map.keySet()){
            DevicePostData mobileData = MobileCache.map.get(key);
            DateTime oldTime = new DateTime(mobileData.getCreatetime());
            Period period = new Period(oldTime,time);
            if(period.getSeconds() > overtime){
                //手机上报超时操作

                //
                MobileCache.map.remove(key);
            }
        }
    }
}
